package com.lankystar.sos.dao.impl;

import java.util.List;
import java.util.Map;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.lankystar.sos.dao.IUserDao;
import com.lankystar.sos.model.User;
import com.lankystar.sos.model.UserApprove;
import com.lankystar.sos.model.UserType;
import com.lankystar.util.MathMethod;

@Repository
public class UserDaoImpl implements IUserDao{

	@Autowired
	private SessionFactory sf ;
	public Session getSession(){
		System.out.println("user开启session！！");
		return sf.getCurrentSession();
	}
	@Override
	public Object[] selectUserByNameAndPass(String userPhone, String userPassword) {
		String sql = "from User u join u.userApprove ua join ua.userType ut where u.userPhone =:userPhone and u.userPassword =:userPassword";
		Object[] ob = (Object[]) getSession().createQuery(sql)
								.setParameter("userPhone", userPhone)
								.setParameter("userPassword", userPassword).uniqueResult();
		User user = (User) ob[0];
		/**
		 * 注册时，生成认证表，用户类型为最基本的   求救者类型
		 */
		System.out.println("userPhone========daodaodaodadoasodos>"+user.getUserPhone()+user.getUserEmail());
		return ob;
	}
	@Override
	public void addUser(User record) {
		 getSession().save(record);
	}
	@Override
	public User selectUserById(Integer userId) {
		User user = (User) getSession().get(User.class, userId);
		return user;
	}
	@Override
	public void addUserApprove(UserApprove record) {
		getSession().save(record);
	}
	@Override
	public UserType selectUserTypeByuserkey(Integer userkey) {
		UserType userType = (UserType) getSession().createQuery("from UserType where userkey=?")
										.setInteger(0, userkey).uniqueResult();
		return userType;
	}
	@Override
	public Object[] selectUserMessageByUserId(Integer userId) {
		String sql = "from UserApprove ua join ua.userType ut join ua.user u where u.userId="+userId+"";
		Object[] ob = (Object[]) getSession().createQuery(sql).uniqueResult();
		return ob;
	}
	@Override
	public User selectUserByuserPhone(String userPhone) {
		User user = (User) getSession().createQuery("from User where userPhone=?")
								.setString(0, userPhone).uniqueResult();
		return user;
	}
	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> selectUserlist(int type,Double longitude, Double latitude) {
		 
		Map<String, Object> map = MathMethod.distance(longitude, latitude);
		String sql = "";
		if(type==1){
			sql = "from ResponseOrder r join r.user u join u.location ul join u.userApprove ua join ua.userType ut where ut.userkey <> 0 and ul.longitude between "+map.get("minlng")+" and "
					+map.get("maxlng")+" and ul.latitude between "+map.get("minlat")+" and "+map.get("maxlat")+"";
		}
		if(type==2){
			sql = "from SosOrder o join o.user u join u.location ul join u.userApprove ua join ua.userType ut where ut.userkey = 0 and ul.longitude between "+map.get("minlng")+" and "
					+map.get("maxlng")+" and ul.latitude between "+map.get("minlat")+" and "+map.get("maxlat")+"";
		}
		/**
		 * 查找到范围内的所有人
		 */
		if(type!=1&&type!=2){
			sql = "from UserLocation ul join ul.user u left join u.userApprove ua join ua.userType ut where ul.longitude between "+map.get("minlng")+" and "
					+map.get("maxlng")+" and ul.latitude between "+map.get("minlat")+" and "+map.get("maxlat")+"";
		}
		List<Object[]> list = getSession().createQuery(sql).list();
		return list;
	}
	
	@Override
	public Object[] selectUserAndUserlocationByUserphone(String userPhone) {
		Object[] ob = (Object[]) getSession().createQuery("from UserLocation ul join ul.user u where u.userPhone=?")
								.setString(0, userPhone).uniqueResult();
		return ob;
	}
	@Override
	public void updateUser(User record) {
		getSession().update(record);
		
	}
	@Override
	public Object[] selectUserApproveAndUserByUserId(Integer userId) {
		String sql = "from User u left join u.userApprove ua join ua.userType ut where u.userId=?";
		Object[] ob = (Object[]) getSession().createQuery(sql)
								.setInteger(0, userId)
								.uniqueResult();
		return ob;
	}
	@Override
	public void updateUserApprove(UserApprove record) {
		getSession().update(record);
	}
	@Override
	public List<Object[]> selectNearUserlist(Integer type, Double longitude, Double latitude) {
		Map<String, Object> map = MathMethod.distance(longitude, latitude);
		String sql = "from  User u left join u.location ul join u.userApprove ua join ua.userType ut where ut.userkey !="+0+" and ul.longitude between "+map.get("minlng")+" and "
				+map.get("maxlng")+" and ul.latitude between "+map.get("minlat")+" and "+map.get("maxlat")+"";
		@SuppressWarnings("unchecked")
		List<Object[]> list = getSession().createQuery(sql).list();
		return list;
		
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Object[]> selectSosOrderByuserPhone(String userPhone) {
		String sql = "from SosOrder sos left join sos.sosStatus status join sos.user u where u.userPhone= "+userPhone+"";
		List<Object[]> oblist = getSession().createQuery(sql).list();
		return oblist;
	}
	

}
